package com.ruoyi.email.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.email.domain.TmdEmailRubbish;
import com.ruoyi.email.service.ITmdEmailPostService;
import com.ruoyi.email.service.ITmdEmailRubbishService;
import com.ruoyi.email.vo.TmdEmailWriteAndSysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.email.domain.TmdEmailWrite;
import com.ruoyi.email.service.ITmdEmailWriteService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.xml.crypto.Data;

/**
 * 写信Controller
 * 
 * @author ruoyi
 * @date 2024-04-20
 */
@Controller
@RequestMapping("/email/write")
public class TmdEmailWriteController extends BaseController
{
    private String prefix = "email/write";

    @Autowired
    private ITmdEmailWriteService   tmdEmailWriteService;
    @Autowired
    private ITmdEmailPostService tmdEmailPostService;
    @Autowired
    private ITmdEmailRubbishService tmdEmailRubbishService;

    @RequiresPermissions("email:write:view")
    @GetMapping()
    public String write()
    {
        return prefix + "/write";
    }

    /**
     * 查询写信列表
     */
    @RequiresPermissions("email:write:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TmdEmailWrite tmdEmailWrite)
    {
        startPage();
        List<TmdEmailWriteAndSysUser> list = tmdEmailWriteService.selectTmdEmailWriteList(tmdEmailWrite);
        return getDataTable(list);
    }

    /**
     * 新增写信
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<SysUser> sysUsers = tmdEmailWriteService.selectUserName();
        model.addAttribute("sysUsers",sysUsers);
        return prefix + "/add";
    }

    /**
     * 新增保存写信
     */
    @RequiresPermissions("email:write:add")
    @Log(title = "写信", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TmdEmailWrite tmdEmailWrite)
    {
        tmdEmailWrite.setCreateTime(DateUtils.getNowDate());
        tmdEmailWrite.setRecivePostTime(DateUtils.getNowDate());
        tmdEmailWrite.setReceiveStatus(1);
        tmdEmailWrite.setPostStaus(1);
        tmdEmailWrite.setStatus(0);
        return toAjax(tmdEmailWriteService.insertTmdEmailWrite(tmdEmailWrite));
    }

    @RequiresPermissions("email:write:add")
    @Log(title = "写信", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(TmdEmailWrite tmdEmailWrite)
    {
        tmdEmailWrite.setDraftStatus(1);
        tmdEmailWrite.setCreateTime(DateUtils.getNowDate());
        return toAjax(tmdEmailWriteService.insertTmdEmailWrite(tmdEmailWrite));
    }

    /**
     * 修改写信
     */
    @RequiresPermissions("email:write:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TmdEmailWrite tmdEmailWrite = tmdEmailWriteService.selectTmdEmailWriteById(id);
        mmap.put("tmdEmailWrite", tmdEmailWrite);
        return prefix + "/edit";
    }

    /**
     * 修改保存写信
     */
    @RequiresPermissions("email:write:edit")
    @Log(title = "写信", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TmdEmailWrite tmdEmailWrite)
    {
        return toAjax(tmdEmailWriteService.updateTmdEmailWrite(tmdEmailWrite));
    }

    /**
     * 删除写信
     */
    @RequiresPermissions("email:write:remove")
    @Log(title = "写信", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] strings = Convert.toStrArray(ids);
        for (String eve:strings) {
            int i = Integer.parseInt(eve);
            TmdEmailWrite tmdEmailWrite = new TmdEmailWrite();
            tmdEmailWrite.setId(i);
            tmdEmailWrite.setReceiveStatus(0);
            tmdEmailWriteService.updateTmdEmailWrite(tmdEmailWrite);
            TmdEmailRubbish tmdEmailRubbish = new TmdEmailRubbish();
            tmdEmailRubbish.setEmailId(i);
            tmdEmailRubbish.setReceiveStatus(1);
            tmdEmailRubbishService.insertTmdEmailRubbish(tmdEmailRubbish);
        }
        return toAjax(1);
    }

    @RequiresPermissions("email:write:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap mmap)
    {
        TmdEmailWrite tmdEmailWrite = new TmdEmailWrite();
        tmdEmailWrite.setStatus(1);
        tmdEmailWrite.setId(id);
        tmdEmailWriteService.updateTmdEmailWrite(tmdEmailWrite);
        TmdEmailWriteAndSysUser tmdEmailWriteAndSysUser = tmdEmailPostService.selectTmdEmailWriteById(id);
        mmap.put("tmdEmailWrite", tmdEmailWriteAndSysUser);
        return prefix + "/detail";
    }
}
